package com.herohua.ad.controller;

import com.alibaba.fastjson.JSON;
import com.herohua.ad.annotation.IgnoreResponseAdvice;
import com.herohua.ad.client.SponsorClient;
import com.herohua.ad.client.vo.AdPlan;
import com.herohua.ad.client.vo.AdPlanRequest;
import com.herohua.ad.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
public class SearchController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SponsorClient sponsorClient;

    @IgnoreResponseAdvice
    @PostMapping("/getAdPlansByRibbon")
    public CommonResponse<List<AdPlan>> getAdPlansByRibbon(AdPlanRequest request) {

        log.info("ad-search: getAdPlansByRibbon -> {} ", JSON.toJSONString(request));

        return restTemplate.postForEntity(
                "http://ad-sponsor/ad-sponsor/get/adPlan",
                request,
                CommonResponse.class
        ).getBody();
    }

    @IgnoreResponseAdvice
    @PostMapping("/getAdPlansByFeign")
    public CommonResponse<List<AdPlan>> getAdPlansByFeign(AdPlanRequest request) {

        log.info("ad-search: getAdPlansByFeign -> {} ", JSON.toJSONString(request));

        return sponsorClient.getAdPlans(request);
    }
}
